<?php
namespace Admin\Controller;
use Think\Page;
use Org\Net\Http;
use Think\Image;
use Common\Controller\AuthController;

class PublishController extends AuthController{
	public function index(){
		$publish = D("PublishView");
	
		$count = $publish->count();
		$page = new Page($count,4);
		$page->setConfig('prev', '上页');
		$page->setConfig('next', '下页');
		$show = $page->show();
		$list = $publish->limit($page->firstRow,$page->listRows)->order(array('ctime'=>'desc'))->select();
	    foreach ($list as $k1=>$v){
	    	$strarr = explode(",",$v['image']);
	    	$list[$k1]['allcontent'] = $v['content'];
	    		foreach ($strarr as $k2=>$new){
	    			$list[$k1]['pic'.$k2]=$new;
	    			//echo $new;
	    		}
	    		if(strlen($v['content'])>60)
	    			$list[$k1]['content']=substr($v['content'], 0,60).'......';
	    }
		$this->assign('list',$list);
		//print_r($list);
		$this->assign('count',$count);
		$this->assign('page',$show);
		$this->display();
	}
	public function del(){
		$publish = D("Publish");
		$id = I('id');
		if($id!=null && $id!=""){
			$http = new Http();
			$image = $publish->where(array('id'=>$id))->getField('image');
			$poid = $publish->where(array('id'=>$id))->getField('poi_id');
			$imageArr = explode(",",$image);
			foreach ($imageArr as $v){
				$v = substr($v,25);
				 if(is_file($v)){
					unlink($v);
				} 
			}
			$post['ak'] = C('BAIDU_AK');
			$post['geotable_id'] = '105582';
			$post['id'] = $poid;
			$data['post'] = $post;
// 			$url = C('BAIDU_POI_DELETE')."?ak=".C('BAIDU_AK')."&geotable_id={$geotable_id}&id={$poid}";
			$result = $http->fsockopenDownload(C('BAIDU_POI_DELETE'),$data);
			$r = json_decode($result);
			 	$publish->delete($id);
			 	M('Discuss')->where(array('pid'=>$id))->delete();
				$this->ajaxReturn(array('status'=>'1'),'json');
		}
		else{
			$this->ajaxReturn(array('status'=>'0'),'json');
		}
	}
	
	//添加约吧
	public function savePublish(){
		$files = $_FILES;
		$time = time();
		$publish_info['content']=I('content');
		$publish_info['time'] = I('time');
		$publish_info['ctime']=time();
		$publish_info['uid'] = I('uid');
		$publish_info['tag'] =I('tag');
		$image_url = '';
		foreach ($files as $file){
			$uploadpath = "Public/uploads/Yue/";//上传路径
				
			$util = A('Util');
			$util->createDir($uploadpath);//上传路径是否已经存在，不存在则创建
	
			$pinfo = pathinfo($file['name']);
			$ftype = $pinfo['extension'];//文件类型
			$name = basename($file['name'],".{$ftype}");
	
			$fileName = md5($name.time()).".".$ftype;//识别图
			$image = new Image();
			$image->open($file['tmp_name']);
			$image->save($uploadpath.$fileName);
			$data["image"][] = $uploadpath.$fileName;
			$image_url .= $uploadpath.$fileName.",";
		}
		$publish_info['image'] = substr($image_url, 0,strlen($image_url)-1);
		$publish = D("Publish");
		if($publish->create($publish_info)){
			$publish->add();
			$this->ajaxReturn(array('result'=>'0','msg'=>'发布成功'),'json');
		}
		else{
			$this->ajaxReturn(array('result'=>'-1','msg'=>$publish->getError()),'json');
		}
	}
}